package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.reggie.common.Result;
import com.it.reggie.dto.SetmealDto;
import com.it.reggie.entity.Category;
import com.it.reggie.entity.Dish;
import com.it.reggie.entity.Setmeal;
import com.it.reggie.entity.SetmealDish;
import com.it.reggie.service.ICategoryService;
import com.it.reggie.service.IDishService;
import com.it.reggie.service.ISetmealDishService;
import com.it.reggie.service.ISetmealService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author:cyh
 * @data:2022/12/16 21:58
 */
@Api(tags = "套餐管理")
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    ISetmealService setmealService;
    @Autowired
    ICategoryService categoryService;

    @Autowired
    ISetmealDishService setmealDishService;

    @Autowired
    IDishService dishService;

    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealWith(setmealDto);
        return Result.success(null);
    }

    @GetMapping("/page")
    public Result<Page> queryByPage(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null&&name!="", Setmeal::getName,name);
        setmealService.page(setmealPage, wrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        List<SetmealDto> setmealDtos=setmealPage.getRecords().stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());


        /*List<SetmealDto> setmealDtos = new ArrayList<>();

        for (Setmeal setmeal : setmealPage.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto );
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);
        }*/
        setmealDtoPage.setRecords(setmealDtos);
        setmealDtoPage.setTotal(setmealPage.getTotal());
        return Result.success(setmealDtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Result remove(@RequestParam("ids") List<Long> ids){
        boolean result=setmealService.deleteSetmeal(ids);
        return result?Result.success(null):Result.filed("套餐正在售卖中,不能删除");
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam("ids") List<Long> ids){
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus, status);
        wrapper.in(Setmeal::getId, ids);
        setmealService.update(wrapper);
        return Result.success(null);
    }

    @GetMapping("/{id}")
    public Result<SetmealDto> queryById(@PathVariable Long id){
        //1、根据套餐id查询套餐数据 select * from setmeal where id =?
        Setmeal setmeal = setmealService.getById(id);
        //2、根据套餐id查询套餐对应的菜品数据 select * from setmeal_dish where setmeal_id = ?
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        //3、将套餐 setmeal 和套餐菜品集合 setmealDishList 数据封装到 SetmealDto并返回
        // 3.0 首先new一个SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        // 3.1、BeanUtils.copyProperties() 拷贝setmeal到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        log.info("setmealDto:{}-----------------------",setmealDto);
        // 3.2、将setmealDishList 设置到 setmealDto中
        setmealDto.setSetmealDishes(setmealDishList);
        log.info("setmealDto:{}-----------------------",setmealDto);
        Category category = categoryService.getById(setmeal.getCategoryId());
        if (category!=null){
            setmealDto.setCategoryName(category.getName());
        }
        //4、返回 setmealDto
        return Result.success(setmealDto);
    }

    @PutMapping
    public Result update(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);

        return Result.success("修改成功");
    }

    //根据套餐分类id查询该套餐分类下的套餐
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#categoryId + '_' + #status",unless = "#result==null")
    public Result<List<Setmeal>> querySetmealById(Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId!=null,Setmeal::getCategoryId,categoryId).eq(status!=null,Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return Result.success(setmealList);
    }

    //客户端点击套餐界面获取套餐的所有菜品
    @GetMapping("/dish/{id}")
    public Result<List<Dish>> setmealDetail(@PathVariable Long id){
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

        List<Dish> dishes = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            Dish dish = dishService.getById(setmealDish.getDishId());
            dishes.add(dish);
        }

        /*Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);*/
        return Result.success(dishes);
    }

}
